import { defineStore } from 'pinia'
import Cache from '@/global/Cache'
import * as CacheKey from '@/global/CacheKey'

export default defineStore('menu', {
  state: () => {
    return {
      menus: Cache.get(CacheKey.MENUS_ROUTER, JSON.stringify([]))
    };
  },
  actions: {
    loadMenus() {
      const currentMenus = Cache.get(CacheKey.MENUS_ROUTER, JSON.stringify([]))
      this.$patch({ menus: JSON.parse(currentMenus) });
    },
    /**
     * 保存角色信息到缓存并更新组件状态
     *
     * @param newState 新的角色数组
     */
    saveMenus(newState: any[]) {
      this.menus = newState;
      Cache.save(CacheKey.MENUS_ROUTER, this.menus);
      // this.$patch({ Menus: newState });
    },
    getMenus(): any[] {
      return Cache.get(CacheKey.MENUS_ROUTER, []);
    },
    resetState() {
      this.$reset();
    },
    clearState() {
      Cache.remove(CacheKey.MENUS_ROUTER);
    }
  }
})